package com.cyy.WolfAnsweringPlatform.job.once;

import com.cyy.WolfAnsweringPlatform.esdao.QuestionEsDao;
import com.cyy.WolfAnsweringPlatform.model.dto.question.QuestionEsDTO;
import com.cyy.WolfAnsweringPlatform.model.entity.Question;
import com.cyy.WolfAnsweringPlatform.service.QuestionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Component
@RequiredArgsConstructor
//@Profile("dev")
//CommandLineRunner执行一次的全量接口，项目启动时自动执行一次
public class FullSyncQuestionToEs implements CommandLineRunner {
    private final QuestionService questionService;
    private final QuestionEsDao questionEsDao;
    @Override
    public void run(String... args) throws Exception {
        List<Question> questionList = questionService.list();
        if(questionList!=null&&questionList.size()>0){
            List<QuestionEsDTO> questionEsDTOList = questionList.stream().map(QuestionEsDTO::objToDto).collect(Collectors.toList());
            int pageSize=500;
            int n=questionEsDTOList.size();
            for(int i=0;i<questionEsDTOList.size();i+=pageSize){
                int end=Math.min(i+pageSize,n);
                questionEsDao.saveAll(questionEsDTOList.subList(i,end));
                log.info("FullSyncQuestionToEs start{}, end {}",i,end);
            }
            log.info("FullSyncQuestionToEs  total {}",questionEsDTOList.size());
        }
    }
}
